CQCIS.workflow.Node = function(config) {
    switch (config.type) {
        case 'start':
            config.shape = 'oval';
            config.name = config.name ? config.name : '开始';
            config.fillcolor = config.fillcolor ? config.fillcolor : '#ff6d15';
            config.strokecolor = config.strokecolor ? config.strokecolor : '#ff6d15';
            config.selFillcolor = config.selFillcolor ? config.selFillcolor : '#fecd8f';
            config.selStrokecolor = config.selStrokecolor ? config.selStrokecolor : '#fecd8f';
            config.width = config.width ? config.width : 50;
            config.height = config.height ? config.height : 50;
            config.textcolor = config.textcolor ? config.textcolor : '#fff';
            config.shadowcolor = config.shadowcolor ? config.shadowcolor : '#e7f1f7';
            config.selTextcolor = config.selTextcolor ? config.selTextcolor : '#db5b0f';
            config.selShadowcolor = config.selShadowcolor ? config.selShadowcolor : '#c8d8e2';
            break;
        case 'end':
            config.shape = 'oval';
            config.name = config.name ? config.name : '结束';
            config.fillcolor = config.fillcolor ? config.fillcolor : '#ebae0b';
            config.strokecolor = config.strokecolor ? config.strokecolor : '#ebae0b';
            config.selFillcolor = config.selFillcolor ? config.selFillcolor : '#ffeb8c';
            config.selStrokecolor = config.selStrokecolor ? config.selStrokecolor : '#ffeb8c';
            config.width = config.width ? config.width : 50;
            config.height = config.height ? config.height : 50;
            config.textcolor = config.textcolor ? config.textcolor : '#fff';
            config.shadowcolor = config.shadowcolor ? config.shadowcolor : '#e7f1f7';
            config.selTextcolor = config.selTextcolor ? config.selTextcolor : '#a97e0d';
            config.selShadowcolor = config.selShadowcolor ? config.selShadowcolor : '#c8d8e2';
            break;
        default:
            config.type = 'node';
            config.shape = 'rect';
            config.name = config.name ? config.name : '新节点';
            config.fillcolor = config.fillcolor ? config.fillcolor : '#f5fafd';
            config.strokecolor = config.strokecolor ? config.strokecolor : '#bed9f2';
            config.selFillcolor = config.selFillcolor ? config.selFillcolor : '#aac1e7';
            config.selStrokecolor = config.selStrokecolor ? config.selStrokecolor : '#7fabd3';
            config.width = config.width ? config.width : 100;
            config.height = config.height ? config.height : 40;
            config.textcolor = config.textcolor ? config.textcolor : '#28437a';
            config.shadowcolor = config.shadowcolor ? config.shadowcolor : '#e7f1f7';
            config.selTextcolor = config.selTextcolor ? config.selTextcolor : '#fff';
            config.selShadowcolor = config.selShadowcolor ? config.selShadowcolor : '#c8d8e2';
            break;
    }
    
    Ext.apply(this, config);
    this.elListeners = this.listeners;
    delete this.listeners;
    
    this.fromId = [];
    this.toId = [];
    
    Ext.ComponentMgr.register(this);
    CQCIS.workflow.Node.superclass.constructor.call(this);
    this.init();
};

Ext.extend(CQCIS.workflow.Node, Ext.util.Observable, {
    id: null,
    name: null,
    number: -1,
    type: 'node',
    shape: 'rect',
    property: null,
    selected: false,
    left: 0,
    top: 0,
    width: 0,
    height: 0,
    mouseX: -1,
    mouseY: -1,
    x: -1,
    y: -1,
    strokeFlag: true,
    shadowFlag: true,
    textFlag: true,
    mirrorFlag: false,
    obj: null,
    shadowObj: null,
    textObj: null,
    strokeObj: null,
    fillcolor: null,
    strokecolor: null,
    textcolor: null,
    shadowcolor: null,
    selFillcolor: null,
    selStrokecolor: null,
    selTextcolor: null,
    selShadowcolor: null,
    rendered: false,
    render: Ext.emptyFn,
    qtip: null,
    getEl: function() {
        return Ext.fly(this.obj);
    },
    init: function() {
        if (this.rendered === false) {
            var obj = document.createElement('v:' + this.shape);
            obj.style.position = 'absolute';
            obj.style.left = this.left;
            obj.style.top = this.top;
            obj.style.cursor = 'pointer';
            obj.style.zIndex = '1000';
            obj.strokeweight = '1';
            obj.fillcolor = this.fillcolor;
            obj.strokecolor = this.strokecolor;
            obj.style.width = this.width;
            obj.style.height = this.height;
            
            if (this.shadowFlag) {
                var shadowObj = document.createElement('v:shadow');
                shadowObj.on = true;
                shadowObj.color = this.shadowcolor;
                obj.appendChild(shadowObj);
                this.shadowObj = shadowObj;
            }
            if (this.strokeFlag) {
                var strokeObj = document.createElement('v:stroke');
                obj.appendChild(strokeObj);
                this.strokeObj = strokeObj;
            }
            if (this.textFlag) {
                var textObj = document.createElement('v:textbox');
                textObj.inset = '5px,5px,5px,5px';
                textObj.innerHTML = this.name;
                Ext.apply(textObj.style, {
                    textAlign: 'center',
                    lineHeight: '20pt',
                    color: this.textcolor
                });
                obj.appendChild(textObj);
                this.textObj = textObj;
            }
            
            this.obj = obj;
            
            this.ownerCt.paletteEl.dom.appendChild(this.obj);
            
            if (!this.mirrorFlag) {
                this.number = this.ownerCt.nodes.length + 1;
                if (this.id === null) {
                    this.id = 'node_' + this.number;
                }
                this.obj.id = this.id;
                this.ownerCt.nodes.push(this);
            } else {
                var fillObj = document.createElement('v:fill');
                this.obj.appendChild(fillObj);
                fillObj.type = "frame";
                fillObj.opacity = ".5";
                this.fillObj = fillObj;
            }
            
            if (this.elListeners) {
                Ext.fly(this.obj).on(Ext.apply(this.elListeners, {
                    scope: this
                }));
                delete this.elListeners;
            }
            
            this.rendered = true;
            
            if (this.qtip) {
                this.qtip = new Ext.ToolTip(Ext.apply(typeof this.qtip == 'object' ? this.qtip : {}, {
                    target: this.obj,
                    html: this.qtip.html || this.name
                }));
            } else if (!this.mirrorFlag) {
                this.obj.title = this.name;
            }
        }
    },
    setDisplay: function(flag) {
        this.obj.style.display = flag;
    },
    pointInObj: function(x, y) {
        var res = false;
        x = this.ownerCt.getX(x);
        y = this.ownerCt.getY(y);
        var x1 = this.left;
        var x2 = x1 + this.width;
        var y1 = this.top;
        var y2 = y1 + this.height;
        if ((x >= x1) && (x <= x2) && (y >= y1) && (y <= y2)) {
            this.mouseX = x;
            this.mouseY = y;
            this.x = x - this.obj.offsetLeft;
            this.y = y - this.obj.offsetTop;
            res = true;
        }
        return res;
    },
    move: function(x, y, mouseX, mouseY) {
        this.left = this.ownerCt.getX(x) - this.x - this.ownerCt.getX(mouseX) + this.mouseX;
        this.top = this.ownerCt.getY(y) - this.y - this.ownerCt.getY(mouseY) + this.mouseY;
        this.obj.style.left = this.left + 'px';
        this.obj.style.top = this.top + 'px';
    },
    moveTo: function(left, top) {
        this.left = left;
        this.top = top;
        this.obj.style.left = this.left + 'px';
        this.obj.style.top = this.top + 'px';
        this.x = 0;
        this.y = 0;
        this.mouseX = this.left;
        this.mouseY = this.top;
    },
    moveEnd: function() {
        this.x = 0;
        this.y = 0;
        this.mouseX = this.left;
        this.mouseY = this.top;
    },
    setSelected: function() {
        if (this.textObj) {
            this.textObj.style.color = this.selTextcolor;
        }
        
        if (this.shadowFlag) {
            this.shadowObj.color = this.selShadowcolor;
        }
        
        this.obj.strokecolor = this.selStrokecolor;
        this.obj.fillcolor = this.selFillcolor;
        this.selected = true;
    },
    clearSelected: function() {
        if (this.textObj) {
            this.textObj.style.color = this.textcolor;
        }
        
        if (this.shadowFlag) {
            this.shadowObj.color = this.shadowcolor;
        }
        
        this.obj.strokecolor = this.strokecolor;
        this.obj.fillcolor = this.fillcolor;
        this.selected = false;
    },
    remove: function() {
        if (this.qtip && this.qtip.destroy) {
            this.qtip.destroy();
        }
        if (this.fromId.length > 0 || this.toId.length > 0) {
            for (var i = this.ownerCt.lines.length - 1; i > -1; i--) {
                var line = this.ownerCt.lines[i];
                if (line.fromNodeId == this.id || line.toNodeId == this.id) {
                    line.remove();
                    this.ownerCt.lines.remove(line);
                    this.ownerCt.selectedObj.remove(line);
                }
            }
        }
        Ext.fly(this.obj).removeAllListeners();
        this.obj.innerHTML = '';
        this.ownerCt.paletteEl.dom.removeChild(this.obj);
    },
    setLeft: function(n) {
        this.left = n;
        this.obj.style.left = n;
    },
    setTop: function(n) {
        this.top = n;
        this.obj.style.top = n;
    },
    setWidth: function(n) {
        this.width = n;
        this.obj.style.width = n;
    },
    setHeight: function(n) {
        this.height = n;
        this.obj.style.height = n;
    },
    getDots: function() {
        var l = this.left;
        var t = this.top;
        var w = this.width;
        var h = this.height;
        var dots = {
            a: {
                x: l,
                y: t + h / 2
            },
            b: {
                x: l + w,
                y: t + h / 2
            },
            c: {
                x: l + w / 2,
                y: t + h
            },
            d: {
                x: l + w / 2,
                y: t
            },
            e: {
                x: l + w / 2,
                y: t + h / 2
            }
        };
        return dots;
    },
    getPosition: function() {
        return {
            top: this.top,
            left: this.left
        };
    },
    setText: function(text, updateQtip) {
        if (this.textFlag) {
            this.name = text;
            this.textObj.innerHTML = this.name;
            if (this.qtip && updateQtip) {
                this.qtip.body.update(this.name);
            } else if (!this.mirrorFlag) {
                this.obj.title = this.name;
            }
        }
    }
});

CQCIS.workflow.Node.prototype.toString = function() {
    return '节点 ' + this.name + '(left: ' + this.left + ', top: ' + this.top + ')';
};
